package com.sun.androidexerciseproject.widget.mvvm.ui.activity;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

import com.sun.androidexerciseproject.widget.R;
import com.sun.androidexerciseproject.widget.mvvm.viewmodel.DemoViewModel;

public class DemoActivity extends AppCompatActivity {

    private Button button;
    private DemoViewModel demoViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        button = findViewById(R.id.btn);
        demoViewModel = ViewModelProviders.of(this).get(DemoViewModel.class);
        demoViewModel.getMyString().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                button.setText(s);
            }
        });
        button.setOnClickListener(v -> demoViewModel.setMyString("测试"));
    }
}
